<template>
  <el-form :model="enroll" :rules="rules" ref="enroll">
    <el-form-item label="邮箱" prop="email" required>
      <el-input type="email" v-model="enroll.email"></el-input>
    </el-form-item>
    <el-form-item label="手机" prop="tel" required>
      <el-input v-model="enroll.tel"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="pass" required>
      <el-input type="password" v-model="enroll.pass" auto-complete="off">
        <span slot="append" v-if="strength">{{ strength }}</span>
      </el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="checkPass" required>
      <el-input type="password" v-model="enroll.checkPass" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-row type="flex" justify="space-around">
        <el-button plain @click="submitForm('enroll')">提交</el-button>
        <el-button plain @click="resetForm('enroll')">重置</el-button>
      </el-row>
    </el-form-item>
  </el-form>
</template>

<script>
import { checkStrong } from '@/utils/weakpass.js'
import { generate } from '@/utils/crypto'
import { AuthPostEnroll } from '@/utils/request'

export default {
  name: 'cl-enroll-bound',
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        let score = checkStrong(value)
        console.log(score)
        if (score >= 70) {
          this.strength = '强'
        } else if (score >= 40) {
          this.strength = '中'
        } else if (score >= 20) {
          this.strength = '弱'
        } else {
          this.strength = ''
          if (score === -1) {
            callback(new Error('请输入密码'))
          } else {
            callback(new Error('此密码不安全，请更换密码'))
          }
        }

        if (this.enroll.checkPass !== '') {
          this.$refs.enroll.validateField('checkPass')
        }

        callback()
      }
    }
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.enroll.pass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      enroll: {
        email: '',
        tel: '',
        pass: '',
        checkPass: ''
      },
      strength: '',
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        tel: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { type: 'string', message: '请输入正确的手机号码', pattern: /^1[\d]{10}$/ }
        ],
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur, change' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let form = {
            email: this.enroll.email,
            tel: this.enroll.tel,
            pass: generate(this.enroll.pass)
          }
          console.info(form)
          let self = this
          AuthPostEnroll(form).then(rsp => {
            const { success } = rsp.data
            if (success) {
              self.$router.push({
                name: 'EnrollProfile'
              })
            } else {
              self.$notify.error({
                title: '注册失败',
                message: rsp.data.msg
              })
            }
          })
        } else {
          this.$notify({
            title: '提交失败',
            message: '请检查表单'
          })
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
